package com.ecommerce.dal.common;

import java.util.List;

import com.ecommerce.dal.entity.Category;
import com.ecommerce.dal.entity.CategoryPhoto;
import com.ecommerce.dal.entity.CategoryPhotoId;
import com.ecommerce.dal.entity.Company;
import com.ecommerce.dal.entity.CompanyPhoto;
import com.ecommerce.dal.entity.CompanyPhotoId;
import com.ecommerce.dal.entity.Item;
import com.ecommerce.dal.entity.ItemPhoto;
import com.ecommerce.dal.entity.ItemPhotoId;
import com.ecommerce.dal.entity.Photo;
import com.ecommerce.dal.service.impl.CategoryManagerService;
import com.ecommerce.dal.service.impl.CompanyManagerService;
import com.ecommerce.dal.service.impl.ItemManagerService;
import com.ecommerce.dal.service.impl.PhotoManagementService;
import com.ecommerce.dal.service.impl.PhotoManagerService;

public class TestPhotoManagement {

	public TestPhotoManagement(PhotoManagementService photoManagementService,
			ItemManagerService itemManagerService,
			CategoryManagerService categoryManagerService,
			CompanyManagerService companyManagerService,
			PhotoManagerService photoManagerService) {
		Photo photo = photoManagerService.retrievePhoto(3L);

		/**
		 * make sure before using ItemPhoto/CompanyPhoto/CategoryPhoto , to
		 * manually edit the fetching strategy in each entity, change it to
		 * EAGER fetching in order to make this code run.
		 * */

		/** -------------------------------------------------------- */
		/** ItemPhoto */
		/** -------------------------------------------------------- */
		ItemPhoto itemPhoto = new ItemPhoto();
		ItemPhotoId itemPhotoId = new ItemPhotoId();// build itemPhotoId
													// manually

		Item item = itemManagerService.retrieveItem(1L);

		itemPhoto.setItem(item);
		itemPhoto.setPhoto(photo);

		/** Build itemPhotoId for itemPhoto */

		itemPhotoId.setItemId(item.getItemId());
		itemPhotoId.setPhotoId(photo.getPhotoId());

		itemPhoto.setId(itemPhotoId);
		/** ----------------------------------------- */
		/** inserting itemPhoto */
		// itemManagerService.updateItem(item);//if you would like to update a
		// selected item or a selected photo.
		// photoManagerService.updatePhoto(photo);
		photoManagementService.saveItemPhoto(itemPhoto);
		/**
		 * ----------------------------------------------/
		 * 
		 * /**Select ItemPhoto
		 */
		List<ItemPhoto> itemPhoto2 = photoManagementService
				.retrieveItemPhotos(1L);
		for (ItemPhoto itemPhotoX : itemPhoto2) {
			System.out.println("Item Name: "
					+ itemPhotoX.getItem().getItemName() + " , Photo number: "
					+ itemPhotoX.getPhoto().getPhotoId() + " , Photo Path: "
					+ itemPhotoX.getPhoto().getPhotoPath());
		}

		/** -------------------------------------------------------- */
		/** CategoryPhoto */
		/** -------------------------------------------------------- */

		Category category = categoryManagerService.retrieveCategory(1L);
		CategoryPhoto categoryPhoto = new CategoryPhoto();

		CategoryPhotoId categoryPhotoId = new CategoryPhotoId();// must build
		// categoryPhotoId
		// manually!

		/** Build categoryPhotoId for itemPhoto */
		categoryPhotoId.setCategoryId(category.getCategoryId());
		categoryPhotoId.setPhotoId(photo.getPhotoId());

		categoryPhoto.setId(categoryPhotoId);
		/** --------------------------------- */
		/** inserting categoryPhoto */
		photoManagementService.saveCategoryPhoto(categoryPhoto);

		/** --------------------------------- */

		/** Select ItemPhoto */
		List<CategoryPhoto> categoryPhoto2 = photoManagementService
				.retrieveCategoryPhotos(1L);
		for (CategoryPhoto categoryPhotoX : categoryPhoto2) {
			System.out.println("Category Name: "
					+ categoryPhotoX.getCategory().getCategoryName()
					+ " , Photo number: "
					+ categoryPhotoX.getPhoto().getPhotoId()
					+ " , Photo Path: "
					+ categoryPhotoX.getPhoto().getPhotoPath());
		}

		/** -------------------------------------------------------- */
		/** CompanyPhoto */
		/** -------------------------------------------------------- */
		Company company = companyManagerService.retrieveCompany(1L);
		CompanyPhoto companyPhoto = new CompanyPhoto();
		CompanyPhotoId companyPhotoId = new CompanyPhotoId();// must build
																// companyPhotoId
																// manually!

		/** Build companyPhotoId for itemPhoto */
		companyPhotoId.setCompanyId(company.getCompanyId());
		companyPhotoId.setPhotoId(photo.getPhotoId());

		companyPhoto.setId(companyPhotoId);
		/** -------------------------------- */
		/** inserting categoryPhoto */
		photoManagementService.saveCompanyPhoto(companyPhoto);
		/** -------------------------------- */

		/** Select CompanyPhoto */
		List<CompanyPhoto> companyPhoto2 = photoManagementService
				.retrieveCompanyPhotos(1L);
		for (CompanyPhoto companyPhotoX : companyPhoto2) {
			System.out.println("Company Name: "
					+ companyPhotoX.getCompany().getCompanyName()
					+ " , Photo number: "
					+ companyPhotoX.getPhoto().getPhotoId() + " , Photo Path: "
					+ companyPhotoX.getPhoto().getPhotoPath());
		}
		/** ------------------ */

	}

}
